import util from '../../utils/util';
import Toast from '@vant/weapp/toast/toast';
import constants from '../../js/const.js';
import {routerFilter} from '../../utils/routerFilter';
routerFilter({

    /**
     * 页面的初始数据
     */
    data: {
        agreeClause: false,
        canObtain:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        const eventChannel = this.getOpenerEventChannel();
        // eventChannel.emit('acceptDataFromOpenedPage', {data: 'test'});
        // eventChannel.emit('someEvent', {data: 'test'});
        // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
        eventChannel.on('acceptDataFromOpenerPage', function (data) {
            that.setData(data);
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getPhoneNumber: function (e) {
        let that = this;
        let data = that.data;
        util.promise('/decryptData', util.extend(data, e.detail)).then(function (res) {
            that.setData(util.extend(data, JSON.parse(res.data)));
        });
    },
    obtainVerifyCode:function(event){
        let that = this;
        let data=that.data;
        if(!data.phoneNumber){
            Toast("请填写手机号码");
            return;
        }
        util.promise('/obtainVerifyCode', data.phoneNumber).then(function (res) {
            if (res.data) {
                Toast("验证码已发送~~");
                that.setData({canObtain: false});
                const countDown = that.selectComponent('.control-count-down');
                countDown.start();
            }
        });
    },
    inserUser: function (e) {
        let that = this;
        let data=that.data;
        if(!data.phoneNumber){
            Toast("请填写手机号码");
            return;
        }
        if(!data.verifyCode){
            Toast("请填写验证码");
            return;
        }
        if(!data.agreeClause){
            Toast("请勾选并查看交运汽学运输协议");
            return;
        }
        that.data.isRequestParam = true;
        util.promise('/insertUser', that.data).then(function (res) {
            if (res.data) {
                util.setStorageSync(constants.cache_key.USER_ID, res.data.userId);
                util.setStorageSync(constants.cache_key.PHONE_NUMBER, res.data.phoneNumber);
                Toast({
                    type: 'success',
                    message: '注册成功',
                    onClose: () => {
                        let pages = getCurrentPages();
                        let prevPage = pages[pages.length -2];//上一页
                        prevPage.onLoad();
                        wx.navigateBack({
                            delta: 1
                        });
                    },
                });
            }
        });
    },
    setCanObtain:function () {
        this.setData({canObtain:true});
    },
    agreeClause(event) {
        this.setData({agreeClause: true});
        this.closeTerms(event);
    }
});